package com.dllo.lanou.cookapp.fragment;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.dllo.lanou.cookapp.R;
import com.dllo.lanou.cookapp.activity.ShowCookingDesActivity;
import com.dllo.lanou.cookapp.adapter.ShowCookAdapter;
import com.dllo.lanou.cookapp.adapter.ShowCookingAdapter;
import com.dllo.lanou.cookapp.entity.AddCookStepEntity;
import com.dllo.lanou.cookapp.entity.Post;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by xijinhua on 2016/3/28.
 */
public class ShowCookingFragment extends Fragment implements AdapterView.OnItemClickListener {
    @Bind(R.id.show_cooking_listview)
    PullToRefreshListView lisview;
    private Handler handler;
    private ShowCookingAdapter adapter;
    private List<AddCookStepEntity> addCookStepEntities = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_showcooking, null);

        ButterKnife.bind(this, view);

        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        adapter = new ShowCookingAdapter(getActivity());
        lisview.setAdapter(adapter);
        queryData();
        handler= new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what ==0){
                  //  addCookStepEntities.clear();
                    addCookStepEntities = (List<AddCookStepEntity>) msg.obj;
                    adapter.addData(addCookStepEntities);

                }

            }
        };


        lisview.setOnItemClickListener(this);
        lisview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                queryData();

//                new LoadDataAsyncTask().execute();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
    }

    public void queryData() {
        BmobQuery<AddCookStepEntity> query = new BmobQuery<AddCookStepEntity>();
        query.setLimit(20);
        query.include("author");// 希望在查询帖子信息的同时也把发布人的信息查询出来
        query.findObjects(getActivity(), new FindListener<AddCookStepEntity>() {
            @Override
            public void onSuccess(List<AddCookStepEntity> lists) {
                Log.i("我得到的数据list", lists + "");
                Collections.reverse(lists);//倒叙
                Message msg = new Message();
                msg.what = 0;
                msg.obj = lists;
                handler.sendMessage(msg);


                Log.i("我得到的数据list", lists + "");
            }

            @Override
            public void onError(int i, String s) {

            }
        });

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        position=position-1;
        Intent intent = new Intent(getActivity(), ShowCookingDesActivity.class);
        AddCookStepEntity addCookStepEntity = addCookStepEntities.get(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("entity",addCookStepEntity);
        intent.putExtras(bundle);
        startActivity(intent);
        Toast.makeText(getActivity(),"第"+position,Toast.LENGTH_LONG).show();
    }

    class LoadDataAsyncTask extends AsyncTask<Void,Void,String> {


        @Override
        protected String doInBackground(Void... params) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "success";
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            if ("success".equals(s)){

                lisview.onRefreshComplete();//刷新完成
            }
        }
    }
}



